package top.dancun.easteacher.feign.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.dancun.common.utils.R;
import top.dancun.easteacher.feign.CourseFeign;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
@Component
public class CourseFeignService {
    @Autowired
    private CourseFeign courseFeign;

    /**
     * 根据课程id获取课程列表
     * @param list
     * @return
     */
    public Map<Integer,String> getCourseByIds(List<Integer> list){
        //访问远程接口
        R r = courseFeign.listByIds(list);
        Map<Integer,String> map = new HashMap<>();
        //非异常返回值
        if (Objects.equals(r.get("code").toString(),"20000")){
            try{
                List<Map<String,Object>> resList = (List<Map<String, Object>>) r.get("data");
                resList.forEach(course->{
                    Integer id = Integer.parseInt(String.valueOf(course.get("id")));
                    String courseName = String.valueOf(course.get("courseName"));
                    map.put(id,courseName);
                });
            }catch (Exception e){
                return map;
            }

        }
        return map;
    }
}
